/*
 * Created on Jun 16, 2009
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ro.qi.framework.sys.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import ro.qi.framework.sys.debug.Logger;

/**
 * @author George Pogorelschi
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class PropertiesConfiguration extends Properties {
	
	private String propertiesFile = "";
	
	public PropertiesConfiguration(String propertiesFile) throws IOException {
		super();
		setPropertiesFile(propertiesFile);
	}

	public void setPropertiesFile(String propertiesFile) throws IOException {
		this.propertiesFile = propertiesFile;

//		InputStream inputFile = this.getClass().getClassLoader().getClass().getResourceAsStream(propertiesFile);
		InputStream inputFile = this.getClass().getResourceAsStream(propertiesFile);
		if (inputFile != null) {
			load(inputFile);
		} else {
			Logger.error(this, "Configuration file \"" + propertiesFile + "\" not found.");
		}
		
	}

	public String getPropertiesFile() {
		return propertiesFile;
	}

	public String getProperty(String propertyName) {
		String value = super.getProperty(propertyName);
		if (value != null) {
			return value;
		} else {
			Logger.warning(this, "Property \"" + propertyName + "\" not defined in file \"" + getPropertiesFile() + "\".");
			return "/* property \"" + propertyName + "\" not defined */";
		}
	}

	public String getStringProperty(String propertyName) {
		return getProperty(propertyName);
	}

	public String getStringProperty(String propertyName, String defaultValue) {
		String propertyValue = getProperty(propertyName);
		if (propertyValue.trim().length() > 0) {
			return propertyValue;
		} else {
			return defaultValue;
		}
	}

	public int getIntProperty(String propertyName, int defaultValue) {
		try {
			return Integer.parseInt(getProperty(propertyName));
		} catch (NumberFormatException exception) {
			return defaultValue;
		}
	}

	public boolean getBooleanProperty(String propertyName, boolean defaultValue) {
		if (getProperty(propertyName).equalsIgnoreCase("true")) {
			return true;
		} else if (getProperty(propertyName).equalsIgnoreCase("false")) {
			return false;
		} else {
			return defaultValue;
		}
	}

	public float getFloatProperty(String propertyName, float defaultValue) {
		try {
			return Float.parseFloat(getProperty(propertyName));
		} catch (NumberFormatException exception) {
			return defaultValue;
		}
	}

}
